﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using ERP.Domain.Team.ProductionSystem;
using ERP.Application.Contracts.ERPSystem.ProductionSystem;
using ERP.Application.Contracts.ERPSystem.ProductionSystem.Dto;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;

namespace ERP.Application.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-设备管理-点检保养项目-xzc
    /// </summary>
    public class SpotCheckServices : ApplicationService, ISpotCheckServices
    {
        private readonly IRepository<SpotChecks, int> _repository;

        public SpotCheckServices(IRepository<SpotChecks, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 新增点检保养项目
        /// </summary>
        /// <param name="spotchecksdto"></param>
        /// <returns></returns>
        public async Task<int> AddSpotChecks(SpotChecksDto spotchecksdto)
        {
            var spotchecks = ObjectMapper.Map<SpotChecksDto, SpotChecks>(spotchecksdto);
            var spotchecksList = await _repository.InsertAsync(spotchecks);
            return spotchecksList == null ? 0 : 1;
        }

        /// <summary>
        /// 修改点检保养项目
        /// </summary>
        /// <param name="spotchecksdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateSpotChecks(SpotChecksDto spotchecksdto)
        {
            var spotchecks = ObjectMapper.Map<SpotChecksDto, SpotChecks>(spotchecksdto);
            var spotchecksList = await _repository.UpdateAsync(spotchecks);
            return spotchecksList == null ? 0 : 1;
        }

        /// <summary>
        /// 查询点检保养项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<SpotChecks>> FindSpotChecks(int id)
        {
            var spotchecksList = await _repository.GetListAsync(m => m.Id == id);
            return spotchecksList;
        }

        /// <summary>
        /// 删除点检保养项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeletedSpotChecks(int id)
        {
            await _repository.DeleteAsync(m => m.Id == id);
        }

        /// <summary>
        /// 获取点检保养项目列表
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="projectName"></param>
        /// <param name="projectType"></param>
        /// <returns></returns>
        public async Task<List<SpotChecks>> GetSpotChecks(string? projectCode, string? projectName, int? projectType)
        {
            var spotchecksList = await _repository.GetListAsync();
            spotchecksList = spotchecksList.WhereIf(!string.IsNullOrEmpty(projectCode), m => m.ProjectCode == projectCode).ToList();
            spotchecksList = spotchecksList.WhereIf(!string.IsNullOrEmpty(projectName), m => m.ProjectName.Contains(projectName)).ToList();
            spotchecksList = spotchecksList.WhereIf(projectType.HasValue, m => m.ProjectType == projectType).ToList();
            return spotchecksList;
        }

        /// <summary>
        /// 批量删除点检保养项目
        /// </summary>
        /// <param name="ids">要删除的ID列表</param>
        /// <returns>成功删除的数量</returns>
        public async Task<int> BatchDeleteSpotChecks(List<int> ids)
        {
            if (ids == null || !ids.Any())
            {
                return 0;
            }

            int successCount = 0;
            foreach (var id in ids)
            {
                try
                {
                    await _repository.DeleteAsync(id);
                    successCount++;
                }
                catch (Exception)
                {
                    // 记录错误但继续处理其他记录
                    continue;
                }
            }

            return successCount;
        }
    }
}
